﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JESAI.HttpRequestMonitor.Dashboard.Implements
{
    public class GlobalAuthorizeFilter : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            if (context.Filters.Any(x => x is IAllowAnonymousFilter))
            {
                return;
            }

            var ActionDescriptor = context.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

            if (ActionDescriptor != null)
            {
                if (ActionDescriptor.MethodInfo.CustomAttributes != null)
                {
                    foreach (var item in ActionDescriptor.MethodInfo.CustomAttributes)
                    {
                        if (item.AttributeType.Name == "AllowAnonymousAttribute")
                        {
                            return;
                        }

                    }

                }

            }



            if (context.HttpContext.Request.Path.HasValue)
            {
                if (!SysConst.CurrentControllers.IsEmpty())
                {
                    var controller = (context.ActionDescriptor.RouteValues["controller"] ?? string.Empty).ToLower();

                    if (!SysConst.CurrentControllers.Split(',').Select(x => x.ToLower()).Contains(controller))
                    {
                        return;
                    }
                }
            }


            string username = context.HttpContext.Request.Cookies[SysConst.LoginCookieId];

            if (string.IsNullOrEmpty(username))
            {
                context.Result = new RedirectResult("/HttpRequestMonitor/UserLogin");
                return;
            }
        }

    }
}
